#-*- test-case-name: fjork.test.test_settings -*-
"""
The settings.py module loads a yaml/json document
which is used as a configuration file
for the fjork service.

Between the ---- markers is an example
of a valid yaml file:

-----------------------------------

# this is a comment
host: localhost
port: 8483

webwares:
  - /tmp/tomato_9000

session_options: {
  expires: 60,
  type: MEMCACHE
}            

-----------------------------------

"""

import exceptions
import os
import sys

from twisted.python import filepath
import yaml


FJORK_ROOT=filepath.FilePath(__file__).parent().parent()
DEBUG=False
HOSTNAME=None
PORT=None
POOL=None
WEBKIT = False

TEST_BIN = FJORK_ROOT.child("bin").child("fjorkChild.py"),
TEMPLATE_DIR = FJORK_ROOT.child("templates").path,


class FjorkSettingsError(exceptions.Exception):
    """Configuration file missing or unable to load all required settings"""


def load(path=None):
    if not path:
        path = os.path.join(os.curdir, "fjork.conf")
        
    if not os.path.exists(path):
        raise FjorkSettingsError("Missing config file: %s"%(path,))

    config_file = open(path, "r")
    config = yaml.safe_load(config_file.read())

    settings = {}
    for key,value in config.iteritems():
        setting = key.upper()
        settings[setting] = value

    print "settings:", settings
    globals().update(settings)
    return settings


load()

